import call from './call.js'

const is = (tokens) => {
  return tokens.peek().type === 'keyword' && tokens.peek().value === 'return'
}

const parse = (tokens) => {
  const node = {
    type: 'ReturnStatement',
    argument: null
  }

  tokens.next()

  if (tokens.peek().type === 'identifier') {
    node.argument = {
      type: 'Identifier',
      name: tokens.peek().value
    }

    tokens.next()

    if (!tokens.eof() && tokens.peek().type === 'punctuation' && tokens.peek().value === '(') {
      node.argument = call.parse(tokens, node.argument.name)
    }

  } else if (tokens.peek().type === 'number' || tokens.peek().type === 'literal') {
    node.argument = {
      type: 'Literal',
      value: tokens.peek().value
    }
    tokens.next()
  } else if (tokens.peek().type === 'keyword' && tokens.peek().value === 'none') {
    node.argument = null
    tokens.next()
  } else {
    tokens.croak(`Can't handle token: ${ tokens.peek().value }`)
  }

  return node
}

export default {
  is,
  parse
}